package com.prs.crm.action.crm;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.crm.CustomerLevel;
import com.prs.crm.domain.crm.LiaisonEvolve;
import com.prs.crm.service.crm.LiaisonEvolveService;
import com.prs.crm.service.sys.LogService;

public class LiaisonEvolveAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private LiaisonEvolve liaisonEvolve;
	private LiaisonEvolveService liaisonEvolveService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private LogService logService;

	
	@Action("listLiaisonEvolve")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				LiaisonEvolve.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getLiaisonEvolveService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editLiaisonEvolve")
	public String edit() {
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "saveLiaisonEvolve", results = {
			@Result(name = "success", type = "redirect", location = "listLiaisonEvolve"),
			@Result(name = "error", type = "chain", location = "editLiaisonEvolve") })
	public String save() {
		if(liaisonEvolve.getName()==null||"".equals(liaisonEvolve.getName().trim())){
			this.addActionError("进度名不能为空");
			return ERROR;
		}if (this.getLogService().getSearchName(LiaisonEvolve.class, liaisonEvolve
				.getName())) {
			this.addActionError("名称已经存在");
			return ERROR;
		}
		this.setPrompt("提交联络进度成功！");
		this.getLiaisonEvolveService().save(liaisonEvolve);
		return SUCCESS;
	}

	@Action(value = "removeLiaisonEvolve", results = {
			@Result(name = "success", type = "redirect", location = "listLiaisonEvolve"),
			@Result(name = "error", type = "chain", location = "listLiaisonEvolve") })
	public String remove() {
		if (ids == null) {
			this.addActionError("请选择要删除的联络进度");
			return ERROR;
		}
		for (Integer id : ids) {
			LiaisonEvolve evolve = this.getLiaisonEvolveService().get(id);
			if (evolve.getLiaisons().size() != 0) {
				this.addActionError("已有客户引用，不能删除");
				return ERROR;
			}
		}
		this.getLiaisonEvolveService().batchRemove(ids);
		return SUCCESS;
	}

	public LiaisonEvolve getLiaisonEvolve() {
		return liaisonEvolve;
	}

	public void setLiaisonEvolve(LiaisonEvolve liaisonEvolve) {
		this.liaisonEvolve = liaisonEvolve;
	}

	public LiaisonEvolveService getLiaisonEvolveService() {
		return liaisonEvolveService;
	}

	public void setLiaisonEvolveService(
			LiaisonEvolveService liaisonEvolveService) {
		this.liaisonEvolveService = liaisonEvolveService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}
}
